Comprehensive Reference Guide
Data types in C specify the type of data that a variable can store. They define:
C language provides four main categories of data types:
Store whole numbers without decimal points.
| Data Type | Size (bytes) | Format Specifier | Example |
|---|---|---|---|
int |
2 or 4 | %d |
int age = 25; |
short |
2 | %hd |
short temp = -10; |
long |
4 or 8 | %ld |
long population = 7800000000L; |
long long |
8 | %lld |
long long big_num = 123456789012LL; |
Store real numbers with decimal points.
| Data Type | Size (bytes) | Format Specifier | Example |
|---|---|---|---|
float |
4 | %f |
float pi = 3.14f; |
double |
8 | %lf |
double precise = 3.1415926535; |
long double |
10-16 | %Lf |
long double very_precise = 3.141592653589793L; |
Store single characters.
| Data Type | Size (bytes) | Format Specifier | Example |
|---|---|---|---|
char |
1 | %c |
char grade = 'A'; |
Represents no type or no value.
| Data Type | Usage | Example |
|---|---|---|
void |
Function return type | void display() { } |
void |
Function parameters | int main(void) { } |
void* |
Generic pointers | void* ptr; |
Collection of elements of the same type.
// Integer array
int numbers[5] = {1, 2, 3, 4, 5};
// Character array (string)
char name[] = "John";
// 2D array
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
Variables that store memory addresses.
int num = 10;
int *ptr = # // ptr stores address of num
char ch = 'A';
char *cptr = &ch;
Block of code that performs specific tasks.
// Function declaration
int add(int a, int b);
// Function definition
int add(int a, int b) {
return a + b;
}
Group different data types together.
struct Student {
int roll_no;
char name[50];
float marks;
};
// Usage
struct Student s1;
s1.roll_no = 101;
strcpy(s1.name, "Alice");
s1.marks = 85.5;
Similar to structures but share memory space.
union Data {
int i;
float f;
char str[20];
};
// Usage
union Data data;
data.i = 10; // Now only i has meaningful value
Define named integer constants.
enum Weekdays {
MONDAY, // 0
TUESDAY, // 1
WEDNESDAY, // 2
THURSDAY, // 3
FRIDAY, // 4
SATURDAY, // 5
SUNDAY // 6
};
// Usage
enum Weekdays today = WEDNESDAY;
Create aliases for existing types.
typedef unsigned int uint;
typedef struct {
int x;
int y;
} Point;
// Usage
uint count = 10;
Point p1 = {10, 20};
signed int a = -10; // Can be positive or negative
signed char b = -128; // Range: -128 to 127
unsigned int x = 40000; // Only positive values
unsigned char y = 255; // Range: 0 to 255
short int s = 100; // Smaller range
long int l = 1000000L; // Larger range
long double ld = 3.14159L; // More precision
const int MAX_VALUE = 100; // Cannot be modified
const float PI = 3.14159f;
volatile int sensor_value; // May change unexpectedly
int* restrict ptr; // No other pointer accesses the same memory
int i = 10;
float f = i; // int to float conversion
char c = 'A';
int ascii = c; // char to int conversion
float f = 3.14f;
int i = (int)f; // f becomes 3
double d = 123.456;
int num = (int)d; // d becomes 123
| Data Type | Typical Size | Range |
|---|---|---|
char |
1 byte | -128 to 127 or 0 to 255 |
unsigned char |
1 byte | 0 to 255 |
signed char |
1 byte | -128 to 127 |
int |
2 or 4 bytes | -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 |
unsigned int |
2 or 4 bytes | 0 to 65,535 or 0 to 4,294,967,295 |
short |
2 bytes | -32,768 to 32,767 |
unsigned short |
2 bytes | 0 to 65,535 |
long |
4 or 8 bytes | -2,147,483,648 to 2,147,483,647 or larger |
unsigned long |
4 or 8 bytes | 0 to 4,294,967,295 or larger |
float |
4 bytes | 1.2E-38 to 3.4E+38 |
double |
8 bytes | 2.3E-308 to 1.7E+308 |
long double |
10-16 bytes | 3.4E-4932 to 1.1E+4932 |
Note: Sizes may vary depending on compiler and architecture.
#include
#include
// Structure example
struct Employee {
int id;
char name[50];
float salary;
};
// Enumeration example
enum Status {
ACTIVE,
INACTIVE,
SUSPENDED
};
int main() {
// Basic types
int age = 25;
float height = 5.9f;
char initial = 'J';
// Array
int scores[5] = {85, 90, 78, 92, 88};
// Structure
struct Employee emp1;
emp1.id = 101;
strcpy(emp1.name, "John Doe");
emp1.salary = 50000.0f;
// Enum
enum Status user_status = ACTIVE;
// Pointer
int *age_ptr = &age;
printf("Age: %d\n", age);
printf("Employee Name: %s\n", emp1.name);
printf("Status: %d\n", user_status);
return 0;
}
const for values that shouldn't changetypedef for complex types to improve readabilitysizeof() operator// Check size of data types
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of double: %zu bytes\n", sizeof(double));
printf("Size of char: %zu bytes\n", sizeof(char));
This comprehensive guide covers all fundamental aspects of data types in C programming, providing a solid foundation for understanding how data is stored and manipulated in the language.